package com.housekeeping.housekeeping.wxPay.controller;

import com.housekeeping.housekeeping.serviceItem.utils.RequestUtils;
import com.housekeeping.housekeeping.serviceItem.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceItem.utils.StatUtil;
import com.housekeeping.housekeeping.wxPay.utils.PayHelper;
import com.housekeeping.housekeeping.wxPay.config.PayConfiguration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/PayServlet/wx")
public class WXPayController extends HttpServlet {
    private static PayHelper payHelper=new PayHelper(new PayConfiguration());
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = RequestUtils.verify(req);
        String order = req.getParameter("order");
        aid="4399";
        //非空校验
        if (aid == null || order == null) {
            ResponseUtils.fail(resp, StatUtil.PARAMS_VALID);
            return;
        }
        try {
            String payUrl = payHelper.createPayUrl(Long.parseLong(order));
            ResponseUtils.success(resp,payUrl,StatUtil.SUCCESS);
        }catch (NumberFormatException e){
            //金额非数字捕抓
            ResponseUtils.fail(resp,StatUtil.PARAMS_VALID);
        }
        catch (Exception e)
        {
            ResponseUtils.fail(resp,StatUtil.INTERNET_ERR);
            e.printStackTrace();
        }
    }
}
